package com.ansion.utils;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.ansion.basetool.R;
import com.ansion.log.VLog;
import com.ansion.utils.dark.setting.DarkBroadcast;
import com.ansion.utils.dark.setting.DarkSetting;


/**
 * @user: dong.wang
 * @data: 2020/12/14 3:30 PM
 * @desc: Activity 最基类
 */
public class ThemeActivity extends AppCompatActivity {

    protected String TAG = "ThemeActivity";

    private DarkBroadcast mDarkBroadcast;
    private int mLastUiMode;

    @Override
    protected void attachBaseContext(Context newBase) {
        Configuration newConfig = new
                Configuration(newBase.getResources().getConfiguration());
        newConfig.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        if (!forbiddenDark()) {
            newConfig.uiMode |= DarkSetting.getInstance().getConfigurationUiMode();
        } else {
            newConfig.uiMode |= Configuration.UI_MODE_NIGHT_NO;
            if (mLastUiMode <= 0) {
                mLastUiMode = DarkSetting.getInstance().getConfigurationUiMode();
            }
        }
        Context configurationContext = newBase.createConfigurationContext(newConfig);
        newBase = configurationContext;
//        ResourceUtils.applyDarkMode(forbiddenDark());
        super.attachBaseContext(newBase);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (VLog.OPEN_LOG)
            VLog.i("ThemeActivity", "onCreate:%s", getClass().getSimpleName());
        TAG = getClass().getSimpleName();

        if (savedInstanceState != null && (Build.VERSION.SDK_INT == 29 || Build.VERSION.SDK_INT == 28)) {
            savedInstanceState.setClassLoader(getClass().getClassLoader());
            Bundle bundle = savedInstanceState.getBundle("androidx.lifecycle.BundlableSavedStateRegistry.key");
            if (bundle != null) {
                for (String key : bundle.keySet()) {
                    Object o = bundle.get(key);
                    if (o instanceof Bundle) {
                        ((Bundle) o).setClassLoader(getClass().getClassLoader());
                    }
                }
            }
        }


        super.onCreate(savedInstanceState);
        if (!forbiddenDark()) {
            mDarkBroadcast = new DarkBroadcast() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    recreate();
                }
            };
            IntentFilter filter = new IntentFilter();
            filter.addAction(DarkBroadcast.DARK_ACTION);
            LocalBroadcastManager.getInstance(this).registerReceiver(mDarkBroadcast, filter);
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            StatusBarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.color_primary));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (null != outState && null != getIntent() && null != getIntent().getExtras()) {
            outState.putAll(getIntent().getExtras());
        }
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mDarkBroadcast) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mDarkBroadcast);
        }
        if (VLog.OPEN_LOG)
            VLog.i("ThemeActivity", "onDestroy:%s", getClass().getSimpleName());
    }


//    protected void changeMode(Configuration configuration) {
//        int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
//        if(!forbiddenDark()) {
//            if(DarkSetting.getInstance().onConfigurationChanged(currentNightMode)) {
//                Intent intent = new Intent(DarkBroadcast.DARK_ACTION);
//                sendBroadcast(intent);
//            }
//        } else {
//            if(mLastUiMode != currentNightMode) {
//                Intent intent = new Intent(DarkBroadcast.DARK_ACTION);
//                sendBroadcast(intent);
//            }
//            mLastUiMode = currentNightMode;
//        }
//
//    }

    /**
     * 是否禁止跟随dark mode 模式切换本页面的模式，禁止后会保持进入该页面之前的模式
     *
     * @return
     */
    protected boolean forbiddenDark() {
        return false;
    }
}
